package com.jh.fcsm.controller.inf;

import com.github.pagehelper.PageInfo;
import com.jh.fcsm.beans.inf.InfMethod;
import com.jh.fcsm.beans.inf.vo.InfMethodVo;
import com.jh.fcsm.common.BaseController;
import com.jh.fcsm.common.RestApiResponse;
import com.jh.fcsm.common.annotation.RepeatSubAnnotation;
import com.jh.fcsm.common.annotation.SystemLogAnnotation;
import com.jh.fcsm.service.inf.InfMethodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 应用令牌权限Controller
 *
 * @author linqiang
 * @date 2022-03-01 14:06:26
 */
@RestController
@RequestMapping("/inf/method")
@Api(value = "应用令牌权限", tags = "应用令牌权限")
public class InfMethodController extends BaseController {
    @Autowired
    private InfMethodService infMethodService;

    private static final String PER_PREFIX = "btn:inf:method:";

    /**
     * @param infMethod 应用令牌权限数据 json
     * @return RestApiResponse<?>
     * @Description: 新增应用令牌权限
     * @Author: linqiang
     */
    @RepeatSubAnnotation
    @PostMapping("/save")
    @ApiOperation(value = "新增应用令牌权限", notes = "新增应用令牌权限")
    @SystemLogAnnotation(type = "应用令牌权限", value = "新增应用令牌权限")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "save')")
    public RestApiResponse<?> save(@RequestBody InfMethod infMethod) {
        String id = infMethodService.saveOrUpdate(infMethod);
        return RestApiResponse.ok(id);
    }

    /**
     * @param infMethod 应用令牌权限数据 json
     * @return RestApiResponse<?>
     * @Description: 修改应用令牌权限
     * @Author: linqiang
     */
    @RepeatSubAnnotation
    @PostMapping("/update")
    @ApiOperation(value = "修改应用令牌权限", notes = "修改应用令牌权限")
    @SystemLogAnnotation(type = "应用令牌权限", value = "修改应用令牌权限")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "update')")
    public RestApiResponse<?> update(@RequestBody InfMethod infMethod) {
        String id = infMethodService.saveOrUpdate(infMethod);
        return RestApiResponse.ok(id);
    }

    /**
     * @param ids
     * @return RestApiResponse<?>
     * @Description: 批量删除应用令牌权限(判断 关联数据是否可以删除)
     * @Author: linqiang
     */
    @RepeatSubAnnotation
    @PostMapping("/delete")
    @ApiOperation(value = "批量删除应用令牌权限", notes = "批量删除应用令牌权限")
    @SystemLogAnnotation(type = "应用令牌权限", value = "批量删除应用令牌权限")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "delete')")
    public RestApiResponse<?> delete(@RequestBody List<String> ids) {
        //判断 关联数据是否可以删除
        infMethodService.delete(ids);
        return RestApiResponse.ok();
    }

    /**
     * @param id
     * @return RestApiResponse<?>
     * @Description: 查询应用令牌权限详情
     * @Author: linqiang
     */
    @GetMapping("/findById")
    @ApiOperation(value = "查询应用令牌权限详情", notes = "查询应用令牌权限详情")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "find')")
    public RestApiResponse<?> findById(@RequestParam("id") String id) {
        InfMethod infMethod = infMethodService.findById(id);
        return RestApiResponse.ok(infMethod);
    }

    /**
     * @return RestApiResponse<?>
     * @Description: 查询所有令牌权限
     * @Author: linqiang
     */
    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有令牌权限", notes = "查询所有令牌权限")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "findAll')")
    public RestApiResponse<?> findAll() {
        List<InfMethod> infMethod = infMethodService.findAll();
        return RestApiResponse.ok(infMethod);
    }

    /**
     * @param infMethodVo 应用令牌权限 查询条件
     * @return RestApiResponse<?>
     * @Description: 分页查询应用令牌权限
     * @Author: linqiang
     */
    @PostMapping("/findPageByQuery")
    @ApiOperation(value = "分页查询应用令牌权限", notes = "分页查询应用令牌权限")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "query')")
    public RestApiResponse<?> findPageByQuery(@RequestBody InfMethodVo infMethodVo) {
        PageInfo<InfMethod> infMethod = infMethodService.findPageByQuery(infMethodVo);
        return RestApiResponse.ok(infMethod);
    }

}
